package com.multiblock.controller;

import com.multiblock.entity.User;
import com.multiblock.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("doadd")
    public boolean doadd(User user, @RequestParam(defaultValue = "-1") Integer[] roleid) {
        boolean flag = false;
        if (user != null) {
            userService.add(user);
            flag = true;
        }
        return flag;
    }

    @RequestMapping("del/{id}")
    public boolean del(@PathVariable int id) {
        boolean flag = false;
        if (id > 0) {
            userService.del(id);
            flag = true;
        }
        return flag;
    }

    @PostMapping("doupdate")
    public boolean doupdate(User user, @RequestParam(defaultValue = "-1") Integer[] roleid) {
        boolean flag = false;
        if (user != null) {
            userService.update(user);
            flag = true;
        }
        return flag;
    }

    @GetMapping("load/{id}")
    public Map<String, Object> load(@PathVariable int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (id > 0) {
            map.put("user", userService.load(id));
        } else {
            map = null;
        }
        return map;
    }

    @GetMapping("list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("users", userService.list());
        return map;
    }

}